﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;

namespace Hisun.View.DataExchange
{
    public partial class CostCenterSelect : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //选择当前选择的记录
        //模板按钮，绑定命领参数，产品 ID，并用符号联接，在主页面再拆开。
        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            string ProductInfo = (string)e.CommandArgument;

            var my = (from a in db.TABC_JS_CostCenter where a.作业中心ID.ToString() == ProductInfo select a).Single();
            ProductInfo = ProductInfo + "&" + my.作业中心名称;


            //string ProductInfo = row.Cells[0].Text + "&"
            //   + row.Cells[1].Text 
            //   + row.Cells[2].Text 
            //    + row.Cells[3].Text;

            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>" + "\n");
            s.Append("window.returnValue='" + ProductInfo.Trim() + "';" + "\n");
            s.Append("window.close();" + "\n");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string csname = "ltype";
            if (!cs.IsStartupScriptRegistered(cstype, csname))
                cs.RegisterStartupScript(cstype, csname, s.ToString());

        }



        //根据查询条件完成产品检索
        // 产品ID 0 产品名称 1 产品辅码 2
        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    var mydata = (from a in db.TABC_JS_CostCenter where a.作业中心ID.ToString().Contains(TextBox_QueryValue.Text) && a.停用.ToString()=="false" select a).Skip(skipAccount).Take(10);

                    GridView_Product.DataSource = mydata;
                    GridView_Product.DataBind();

                    break;
                case 1:

                    var mydata1 = (from a in db.TABC_JS_CostCenter where a.作业中心名称.Contains(TextBox_QueryValue.Text) && a.停用.ToString() == "false" select a).Skip(skipAccount).Take(10);

                    GridView_Product.DataSource = mydata1;
                    GridView_Product.DataBind();
                    break;

                //default:
                //    var mydata2 = (from a in db.TABC_DM_Product where a.产品ID.Contains(TextBox_QueryValue.Text) select a).Skip(skipAccount).Take(10);

                //    GridView_Product.DataSource = mydata2;
                //    GridView_Product.DataBind();
                //    break;

            }


        }


        //翻页的功能
        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    MyQuery = (from a in db.TABC_JS_CostCenter where a.作业中心ID.ToString().Contains(TextBox_QueryValue.Text) && a.停用.ToString() == "false" select a).Count();
                    break;
                case 1:
                    MyQuery = (from a in db.TABC_JS_CostCenter where a.作业中心名称.Contains(TextBox_QueryValue.Text) && a.停用.ToString() == "false" select a).Count();

                    break;
            } 

            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);


        }
    }
}
